package com.xxcxcxcx.jobsystem.mixin.job.Miner;

import com.xxcxcxcx.jobsystem.PlayerSaveData;
import com.xxcxcxcx.jobsystem.common.Job.Miner.Miner;
import com.xxcxcxcx.jobsystem.common.JobsClass;
import com.xxcxcxcx.jobsystem.common.capability.JobCapability;
import com.xxcxcxcx.jobsystem.common.capability.JobCapabilityProvider;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.util.LazyOptional;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import javax.annotation.Nullable;

@Mixin(Block.class)
public class MinerBlockMixin {
    @ModifyConstant(method = "playerDestroy", constant = @Constant(floatValue = 0.005F, ordinal = 0))
    private float originsClasses$removeBlockMiningExhaustion(float exhaustion, Level world, Player playerEntity, BlockPos pos, BlockState state, BlockEntity blockEntity, ItemStack stack) {
        LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(playerEntity);
        if(!capability.isPresent()) {
            return exhaustion;
        }
        PlayerSaveData playerSaveData = capability.orElse(null);
        if(playerSaveData.getLevel(JobsClass.Miner)>=7) {
            return 0;
        }
        return exhaustion;
    }
    @Inject(method = "playerDestroy", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/Block;dropResources(Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/Level;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/entity/BlockEntity;Lnet/minecraft/world/entity/Entity;Lnet/minecraft/world/item/ItemStack;)V"))
    private void originsClasses$additionalDrop(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack stack, CallbackInfo ci) {
        if(Miner.GetRightBlock(state)){
            Miner.DropMore(state,world,pos,blockEntity,player,stack);
        }
    }
}
